/**
 * @ClassName : _748_最短补全词_write
 * @Author : 骆发茂
 * @Date: 2021/12/10 20:44
 * @Description : https://leetcode-cn.com/problems/shortest-completing-word/
 */
public class _748_最短补全词_lfm_write {
    class Solution {
        public String shortestCompletingWord(String licensePlate, String[] words) {
            int[] lCount = new int[26];
            for (int i = 0; i < licensePlate.length(); i++) {
                if (Character.isLetter(licensePlate.charAt(i))) {
                    lCount[Character.toLowerCase(licensePlate.charAt(i)) - 'a']++;
                }
            }

            String answer = null;
            for (String word :
                    words) {
                //判断条件：是符合补全词，而且是最短补全词
                if (check(word, lCount) && (answer == null || answer.length() > word.length())) {
                    answer = word;
                }
            }
            return answer;
        }

        private boolean check(String word, int[] lCount) {
            int[] wordCount = new int[26];
            for (int i = 0; i < word.length(); i++) {
                wordCount[Character.toLowerCase(word.charAt(i)) - 'a']++;
            }
            for (int i = 0; i < 26; i++) {
                if (lCount[i] > wordCount[i]) {
                    return false;
                }
            }


            return true;
        }
    }
}